/**
 * Created by seeker910 on 14-1-1.
 * label:{width:'auto'}
 */
Rsd.define('Rsd.form.CheckBox', {
    extend: 'Rsd.form.Input',
    xtype: 'checkbox',
    height: 30,
    width: 80,
    margin: '4 0 4 0',
    inputType: 'CheckBox',
    ctrlCls: 'x-control-checkbox',
    checked:false,
    /**
     * value 模式：check or value
     */
    mode:'value',//
    /**
     * 
     * @param {*} config 
     */
    constructor: function CheckBox(config) {
        config = config || {};
        this.apply(config);
    },
    /**
     * 
     */
    initComponentEx: function initComponentEx() {
        this.callParent();

        var _label = document.createElement('label');
        _label.setAttribute('for',this.ctrl.element.id); 
 
        this.setElStyle(_label,{
            position:"relative",
            display:"inline",
            overflow:"hidden",
            minHeight:28,
            minWidth:28,
            maxWidth:35,
            maxHeight:35,  
            lineHeight:this.height,
            padding:(this.height-30)/2 + 'px', 
            height:'auto',
            width: 'auto'
        }); 
        if(this.readonly)
        {
            this.setElStyle(_label,{pointerEvents:"none"}); 
        }
        this.__self_label = _label;

        this.setElStyle(this.ctrl.element,{position:'absolute',visibility:'hidden',overflow:'hidden'}); 
        
    },
    /*
    * */
    onAfterInit: function onAfterInit() {

        this.callParent();  
        this.check(this.checked); 
    },
    /*
    * */
    onAfterRender:function onAfterRender()
    { 
        this.callParent();
        this.setElStyle(this.ctrl.element,{height:0,width:0});
        this.dom.appendChild(this.__self_label); 
        //console.log('onAfterRender ' + this.name)
        
    },
     
    /***
     * checked 和 value 是两个属性值
     */
    getValue: function getValue() {
       
        if(this.mode == 'value')
        {
            return this.isChecked() ? this.callParent():'';
        }else
        {
            return this.isChecked();
        }
        
    },
    /**
     * 
     */
    setValue:function setValue(value)
    {
        if(this.mode == 'value')
        {
            this.callParent(value);  
        }
        else
        {
            this.check(value);
        }
    },
    /**
     * 
     */
    onAfterLayout:function onAfterLayout() {

        this.callParent();
 
        //调整 label 布局
        var _label = this.__self_label;
        //_label.style.lineHeight = _label.clientWidth + 'px';

        switch (this.label.position) {
            case 'top':
                {
                    _label.style.marginBottom = this.label.space + 'px'; 
                    break;
                }
            case 'bottom':{
                _label.style.marginTop = this.label.space + 'px'; 
                break;
            }
            case 'right': { 
                _label.style.marginRight = this.label.space + 'px';
                break;
            }

            case 'left':
            default: { 
                _label.style.marginLeft = this.label.space + 'px';
                break;
            }
        }

    },
    /*
    * checked 和 value 是两个属性值
    */
    isChecked:function isChecked()
    {
        return this.ctrl.element ? this.ctrl.element.checked : this.checked;
    },

    /*
    *
    * */
    check:function check(check)
    {
        this.checked = check;

        if(this.ctrl.element)
        {
            this.ctrl.element.checked = check;
        }
        
        return check;
    },
    /*
     * 返回 对象或数组
     * */
    makeControl:function makeControl(config,row)
    {
        var _config = config ||{};
        var _editable = _config.editable;
        var _dataIndex = _config.dataIndex|| _config.dataindex || _config.name;
        var _value = row[_dataIndex];
         
        var _chk = document.createElement('input');

        _chk.classList.add('x-control-checkbox');
        _chk.style.visibility = 'hidden';
        _chk.style.position ='absolute';
        _chk.type = 'checkbox';
        _chk.name = _dataIndex;
        //console.log(config,row);
        if(!config.keyMember)
        {
            console.error('CheckBox Type column must be have [keyMember] config.',config);
        }
        _chk.setAttribute('id','__chk__' + _dataIndex + '__' + row[config.keyMember]);
        var _table = config.table;
        _chk.checked = Rsd.isTrue(_value); 
        _chk.onchange = function () {
            //单选
            if (config.multiple == false && this.checked) {

                for (var i in _table.rows) {
                    var _tbRow =  _table.rows[i];
                    _tbRow[_dataIndex] = false;
                } 
              
            }
            _table.rows[this.parentNode.parentNode.id][_dataIndex] = this.checked;

            _table.refresh(_dataIndex);
        };

        var _label = document.createElement('label'); 
        Rsd.setElStyle(_label,{
            position:"relative", 
            overflow:"hidden",
            minHeight:24,
            minWidth:24,
            maxWidth:28,
            maxHeight:28,  
            lineHeight: (_chk.clientHeight||24) + 'px',
            height:'auto',
            width: 'auto'
        });
        _label.setAttribute('for',_chk.getAttribute('id'));

        if(_editable)
        { 
        }
        else
        {
            _label.onclick=function () {
                return false;
            };

        }
        return [_chk,_label];

    }
});

